package com.eorchis.module.trainingneeds.ui.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.eorchis.core.service.IBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.commond.converter.IQueryCommondAttributeConverter;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.classcourse.service.IClassCourseService;
import com.eorchis.module.classcourse.ui.commond.ClassCourseValidCommond;
import com.eorchis.module.trainingclass.service.ITrainingClassService;
import com.eorchis.module.trainingclass.ui.commond.TrainingClassValidCommond;
import com.eorchis.module.trainingneeds.domain.PreviewQuestionnaireBean;
import com.eorchis.module.trainingneeds.domain.QueryQuestionBankBean;
import com.eorchis.module.trainingneeds.domain.QueryQuestionnaireBean;
import com.eorchis.module.trainingneeds.domain.QuerySurveyObjectBean;
import com.eorchis.module.trainingneeds.domain.QuestionTypeBean;
import com.eorchis.module.trainingneeds.domain.Questionnaire;
import com.eorchis.module.trainingneeds.domain.QuestionnaireSurveyObject;
import com.eorchis.module.trainingneeds.service.IQuestionnaireService;
import com.eorchis.module.trainingneeds.ui.commond.QuestionBankQueryCommond;
import com.eorchis.module.trainingneeds.ui.commond.QuestionBankValidCommond;
import com.eorchis.module.trainingneeds.ui.commond.QuestionnaireQueryCommond;
import com.eorchis.module.trainingneeds.ui.commond.QuestionnaireQuestionTypeValidCommond;
import com.eorchis.module.trainingneeds.ui.commond.QuestionnaireSurveyObjectQueryCommond;
import com.eorchis.module.trainingneeds.ui.commond.QuestionnaireSurveyObjectValidCommond;
import com.eorchis.module.trainingneeds.ui.commond.QuestionnaireValidCommond;
import com.eorchis.module.user.domain.User;
import com.eorchis.util.PathUtil;
import com.eorchis.util.TwoDimensionCode;
import com.eorchis.utils.BeanUtils;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: QuestionnaireController<br>
 * Description: 培训需求问卷表管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author HuangTF
 * @createDate 2014-02-07
 * @version $Revision$
 */
@Controller("questionnaireController")
@RequestMapping(value = QuestionnaireController.MODULE_PATH)
public class QuestionnaireController extends AbstractBaseController<QuestionnaireValidCommond,QuestionnaireQueryCommond> {
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/questionnaire";
	// 培训需求问卷表接口
	@Resource(name = "com.eorchis.module.trainingneeds.service.impl.QuestionnaireServiceImpl")
	private IQuestionnaireService questionnaireService;

	// 班级课程维护接口
	@Autowired
	@Qualifier("com.eorchis.module.classcourse.service.impl.ClassCourseServiceImpl")
	private IClassCourseService classCourseService;
		
	// 培训班信息接口
	@Autowired
	@Qualifier("com.eorchis.module.trainingclass.service.impl.TrainingClassServiceImpl")
	private ITrainingClassService trainingClassService;

	@Autowired
	@Qualifier("attributeConverter")
	private ArrayList<IQueryCommondAttributeConverter> attributeConverter;
	
	@Override
	public IBaseService getService() {
		return questionnaireService;
	}

	@Override
	public String getModulePath() {
		return MODULE_PATH;
	}
	
	/**
	 * 查询培训需求调查问卷列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/findQuestionnaireList")
	public String findQuestionnaireList(
			@ModelAttribute("resultList") QuestionnaireQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		if (attributeConverter != null) {
			parameterValueConver(queryCommond, request, attributeConverter);
		}
		
		queryCommond.setActiveState(Questionnaire.IS_ACTIVE_Y);//查询活动状态为活动的
		List<QueryQuestionnaireBean> list=(List<QueryQuestionnaireBean>) questionnaireService.findList(queryCommond);
		queryCommond.setResultList(list);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 新增培训需求调查问卷
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@RequestMapping(value = "/addQuestionnaire")
	public String addQuestionnaire(
			@ModelAttribute("result") @Valid QuestionnaireValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		//判断问卷名称是否重复
		if(questionnaireService.isRepeatName(commond.getQuestionnaireName()))
		{
			resultState.setMessage("问卷名称已经存在");
			return "";
		}
		com.eorchis.module.department.domain.Department department=(com.eorchis.module.department.domain.Department)request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		com.eorchis.module.user.domain.User user=(com.eorchis.module.user.domain.User)request.getSession().getAttribute(SessionConstant.USER_INFO);
//		User user=new User();user.setUserId("3966084");
//		Department department=new Department();department.setDeptID("1822315");
		
		commond.setUser(user);
		commond.setDepartment(department);
		commond.setPublishState(Questionnaire.IS_PUBLISH_N);
		questionnaireService.save(commond);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("新增问卷成功");
		return "";
	}
	/**
	 * 查询培训需求调查问卷信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@RequestMapping(value = "/queryQuestionnaireInfo")
	public String queryQuestionnaireInfo(
			@ModelAttribute("result") @Valid QuestionnaireValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		commond=questionnaireService.find(commond.getEntityID());
		
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 更新培训需求调查问卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@RequestMapping(value = "/updateQuestionnaire")
	public String updateQuestionnaire(
			@ModelAttribute("result") @Valid QuestionnaireValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
					{
		QuestionnaireValidCommond c=questionnaireService.find(commond.getEntityID());
		
		c.setQuestionnaireName(commond.getQuestionnaireName());
		
		BaseData questionnaireType=new BaseData();
		questionnaireType.setDataCode(commond.getQuestionnaireType());
		
		((Questionnaire)c.toEntity()).setQuestionnaireType(questionnaireType);
		c.setSurveyType(commond.getSurveyType());
		c.setBeginDate(commond.getBeginDate());
		c.setEndDate(commond.getEndDate());
		c.setQuestionnaireExplain(commond.getQuestionnaireExplain());
		questionnaireService.update(c);
		
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("更新成功");
		return "";
					}
	/**
	 * 发布撤销培训需求调查问卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@RequestMapping(value = "/publishQuestionnaire")
	public String publishQuestionnaire(
			@ModelAttribute("resultList") QuestionnaireQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		String mess=questionnaireService.publishQuestionnaire(queryCommond);
		resultState.setMessage(mess);
		return "";
	}
	/**
	 * 逻辑删除培训需求调查问卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@RequestMapping(value = "/delQuestionnaire")
	public String delQuestionnaire(
			@ModelAttribute("resultList") QuestionnaireQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		questionnaireService.delQuestionnaire(queryCommond);
		return "";
	}
	/**
	 * 查询已选调查对象(指定人员)
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-27
	 */
	@RequestMapping(value = "/querySurveyObjectSelected")
	public String querySurveyObjectSelected(
			@ModelAttribute("resultList") QuestionnaireSurveyObjectQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QuerySurveyObjectBean> userList=questionnaireService.querySurveyObjectSelected(queryCommond);
		
		queryCommond.setResultList(userList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("查询成功");
		
		return "";
	}
	/**
	 * 查询未选调查对象(指定人员)
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-27
	 */
	@RequestMapping(value = "/querySurveyObjectNoSelected")
	public String querySurveyObjectNoSelected(
			@ModelAttribute("resultList") QuestionnaireSurveyObjectQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QuerySurveyObjectBean> userList=questionnaireService.querySurveyObjectNoSelected(queryCommond);
		
		queryCommond.setResultList(userList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("查询成功");
		
		return "";
	}
	/**
	 * 设置问卷调查对象类型
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-27
	 */
	@RequestMapping(value = "/setSurveyObjectType")
	public String setSurveyObjectType(
			@ModelAttribute("result") @Valid QuestionnaireSurveyObjectQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		//设置类型前先清除上一次的设置信息
		questionnaireService.delSurveyObjectType(queryCommond);
		
		if(queryCommond.getSurveyObjectType().intValue()==QuestionnaireSurveyObject.SURVEY_TYPE_ALL)
		{
			QuestionnaireSurveyObjectValidCommond commond=new QuestionnaireSurveyObjectValidCommond();
			commond.setQuestionnaire(new Questionnaire(queryCommond.getQuestionnaireId()));
			commond.setWriteType(1);//填写方式    默认1:学员填写  
			commond.setSurveyObjectType(queryCommond.getSurveyObjectType());
			questionnaireService.save(commond);
		}
//		else
//			if(queryCommond.getSurveyObjectType().intValue()==QuestionnaireSurveyObject.SURVEY_TYPE_USER)
//			{
//				questionnaireService.updateSurveyObjectType(queryCommond);
//			}
		//更新问卷表调查对象类型
		questionnaireService.updateSurveyObjectTypeQuestionnaire(queryCommond);
		
		resultState.setMessage("设置成功");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 添加指定调查对象
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-27
	 */
	@RequestMapping(value = "/addUserToSurveyObject")
	public String addUserToSurveyObject(
			@ModelAttribute("resultList") QuestionnaireSurveyObjectQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		QuestionnaireSurveyObjectValidCommond commond=new QuestionnaireSurveyObjectValidCommond();
		commond.setQuestionnaire(new Questionnaire(queryCommond.getQuestionnaireId()));
		commond.setWriteType(1);//填写方式    默认1:学员填写  
		if(PropertyUtil.objectNotEmpty(queryCommond.getSurveyObjectIds()))
		{
			String[] ids=queryCommond.getSurveyObjectIds();
			for(String id: ids)
			{
				commond.setQuestionnaireSurveyObjectId(null);
				commond.setSurveyObjectId(id);
				commond.setSurveyObjectType(QuestionnaireSurveyObject.SURVEY_TYPE_USER);
				questionnaireService.save(commond);
			}
		}
		resultState.setMessage("添加成功");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 删除指定调查对象
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-27
	 */
	@RequestMapping(value = "/delUserFromSurveyObject")
	public String delUserFromSurveyObject(
			@ModelAttribute("resultList") QuestionnaireSurveyObjectQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		questionnaireService.delSurveyObject(queryCommond);
		resultState.setMessage("删除成功");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 查询题库试题列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-17
	 */
	@RequestMapping(value = "/queryQuestionBank")
	public String queryQuestionBank(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QueryQuestionBankBean> beanList=questionnaireService.queryQuestionBank(queryCommond);
		queryCommond.setResultList(beanList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		
		return "";
	}
	/**
	 * 新增题库试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-17
	 */
	@RequestMapping(value = "/addQuestionBank")
	public String addQuestionBank(
			@ModelAttribute("result") @Valid QuestionBankValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		resultState.setState(ResultState.RESULT_STATE_FAIL);//默认状态失败
		//判断试题题干是否重复
		if(questionnaireService.isRepeatQuestionName(commond))
		{
			resultState.setMessage("题干重复，请修改！");
			return "";
		}
		if(token.isTokenValid(request, true))//判断token
		{
			String userId=((User)request.getSession().getAttribute(SessionConstant.USER_INFO)).getUserId();
			User user=new User();
			user.setUserId(userId);
			commond.setUser(user);
			String mess=questionnaireService.addQuestionBank(commond);
			
			resultState.setMessage(mess);
			if("添加成功".equals(mess))
			{
				//设置问卷试题时，添加问卷试题关系信息
				if(PropertyUtil.objectNotEmpty(commond.getQuestionnaireId()))
				{
					QuestionBankQueryCommond queryCommond=new QuestionBankQueryCommond();
					String[] questionIds= new String[1];
					questionIds[0]=commond.getEntityID().toString();
					
					queryCommond.setQuestionIds(questionIds);
					queryCommond.setQuestionnaireId(commond.getQuestionnaireId());
					
					questionnaireService.addQuestionnaireQuestion(queryCommond);
				}
				
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			}
		}
		else
		{
			resultState.setState(ResultState.RESULT_STATE_REPEAT_SUBMIT);//重复提交
			resultState.setMessage("重复提交");
		}
		return "";
	}
	/**
	 * 查询试题信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-17
	 */
	@RequestMapping(value = "/queryQuestionInfo")
	public String queryQuestionInfo(
			@ModelAttribute("resultList") QuestionBankValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		QueryQuestionBankBean bean=questionnaireService.queryQuestionInfo(commond);
		commond.setIsScoreType(bean.getIsScoreType());
		commond.setQuestionId(bean.getQuestionId());
		commond.setQuestionName(bean.getQuestionName());
		commond.setQuestionType(bean.getQuestionType());
		commond.setWordNum(bean.getWordNum());
		commond.setOptionList(bean.getQuestionOption());
		commond.setQuestionTypes(bean.getIsScoreType());
		commond.setQuestionsProblemSets(bean.getQuestionsProblemSets());
		commond.setObjectiveTopic(bean.getObjectiveTopic());
		if(PropertyUtil.objectNotEmpty(commond.getWordNum()))
		{
			commond.setIsLimitNum("1");
		}
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 更新题库试题
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-17
	 */
	@RequestMapping(value = "/updateQuestion")
	public String updateQuestion(
			@ModelAttribute("resultList") QuestionBankValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		resultState.setState(ResultState.RESULT_STATE_FAIL);//默认状态失败
		//判断试题题干是否重复
		if(questionnaireService.isRepeatQuestionName(commond))
		{
			resultState.setMessage("题干重复，请修改！");
			return "";
		}
		if(token.isTokenValid(request, true))//判断token
		{
			String mess=questionnaireService.updateQuestionBank(commond);
			resultState.setMessage(mess);
			if("更新成功".equals(mess))
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}
		else
		{
			resultState.setState(ResultState.RESULT_STATE_REPEAT_SUBMIT);//重复提交
			resultState.setMessage("重复提交");
		}
		return "";
	}
	/**
	 * 删除题库试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-17
	 */
	@RequestMapping(value = "/delQuestionBank")
	public String delQuestionBank(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		questionnaireService.delQuestionBank(queryCommond);
		resultState.setMessage("删除成功");
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		
		return "";
	}
	/**
	 * 查询问卷表题型
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/queryQuestionType")
	public String queryQuestionType(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QuestionTypeBean> types=questionnaireService.queryQuestionType(queryCommond);
		queryCommond.setResultList(types);
		
		return "";
	}
	/**
	 * 问卷设置试题时查询已选题库试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/queryQuestionSelected")
	public String queryQuestionSelected(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QueryQuestionBankBean> beanList=questionnaireService.queryQuestionSelected(queryCommond);
		queryCommond.setResultList(beanList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 问卷设置试题时查询未选题库试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/queryQuestionNoSelected")
	public String queryQuestionNoSelected(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QueryQuestionBankBean> beanList=questionnaireService.queryQuestionNoSelected(queryCommond);
		queryCommond.setResultList(beanList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 新增问卷试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/addQuestionnaireQuestion")
	public String addQuestionnaireQuestion(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		questionnaireService.addQuestionnaireQuestion(queryCommond);
		return "";
	}
	/**
	 * 更新问卷试题是否必填及排序
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/updateQuestionnaireQuestion")
	public String updateQuestionnaireQuestion(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		questionnaireService.updateQuestionnaireQuestion(queryCommond);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 保存问卷试题题型
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/saveQuestionnaireQuestionType")
	public String saveQuestionnaireQuestionType(
			@ModelAttribute("result") @Valid QuestionnaireQuestionTypeValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		questionnaireService.saveQuestionnaireQuestionType(commond);
		return "";
	}
	/**
	 * 删除问卷试题
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-18
	 */
	@RequestMapping(value = "/delQuestionnaireQuestion")
	public String delQuestion(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		if(PropertyUtil.objectNotEmpty(queryCommond.getQuestionnaireQuestionIds()))
		{
			questionnaireService.delQuestionnaireQuestion(queryCommond);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}
		return "";
	}
	/**
	 * 用于排序的方法
	 * @param queryCommond
	 * @param request
	 * @param attributeConverter
	 */
	@SuppressWarnings("unchecked")
	private void parameterValueConver(QuestionnaireQueryCommond queryCommond,
			HttpServletRequest request,
			List<IQueryCommondAttributeConverter> attributeConverter) 
	{
		for (IQueryCommondAttributeConverter queryCommondAttributeConverter : attributeConverter) 
		{
			queryCommondAttributeConverter.convert(queryCommond,request.getParameterMap());
		}
	}
	/**
	 * 问卷查看列表,查看已结束问卷调查情况
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-4-29
	 */
	@RequestMapping(value = "/findEndQuestionnaireList")
	public String findEndQuestionnaireList(
			@ModelAttribute("resultList") QuestionnaireQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QueryQuestionnaireBean> list=questionnaireService.findEndQuestionnaireList(queryCommond);
		queryCommond.setResultList(list);
		return "";
	}
	/**
	 * 查看参与问卷调查的人员详情
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-4-29
	 */
	@RequestMapping(value = "/findSurveyUserInfo")
	public String findSurveyUserInfo(
			@ModelAttribute("resultList") QuestionnaireSurveyObjectQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QuerySurveyObjectBean> resultList=questionnaireService.findSurveyUserInfo(queryCommond);
		queryCommond.setResultList(resultList);
		return "";
	}
	/**
	 * 查看已结束问卷主观题调查情况
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-4-29
	 */
	@RequestMapping(value = "/endQuestionnaireSubjective")
	public String endQuestionnaireSubjective(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QueryQuestionBankBean> list=questionnaireService.endQuestionnaireSubjective(queryCommond);
		queryCommond.setResultList(list);
		return "";
	}
	/**
	 * 查看已结束问卷客观题调查情况
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-4-29
	 */
	@RequestMapping(value = "/endQuestionnaireObjective")
	public String endQuestionnaireObjective(
			@ModelAttribute("resultList") QuestionBankQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<QueryQuestionBankBean> list=questionnaireService.endQuestionnaireObjective(queryCommond);
		queryCommond.setResultList(list);
		return "";
	}
	
	/**
	 * 预览问卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-28
	 */
	@RequestMapping(value = "/previewQuestionnaire")
	public String previewQuestionnaire(
			@ModelAttribute("result") QuestionnaireValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		PreviewQuestionnaireBean previewQuestionnaireBean=questionnaireService.queryQuestionnaireQuestion(commond);
		
		commond.setPreviewQuestionnaireBean(previewQuestionnaireBean);
		commond.setUser(null);
		
		return "";
	}
	/**
	 * 导入Excel试题到题库
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-11-06
	 */
	@RequestMapping(value = "/importExcelQuestion")
	public String importExcelQuestion(@ModelAttribute("result") QuestionnaireValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception
	{
//		resultState.setState(ResultState.RESULT_STATE_FAIL);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		
		for(MultipartFile multipartFile : fileMap.values())
		{
			CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)multipartFile;
		    FileItem item=commonsMultipartFile.getFileItem();
		    if(!(item.isFormField()))
		    {
		    	if(item.getSize()==0l)
		    	{
		    		resultState.setMessage("文件不能为空！");
		    		return "";
		    	}
		    	if(PropertyUtil.objectNotEmpty(item.getName()))
		    	{
		    		String fileName=item.getName();
		    		String fileType=fileName.substring(fileName.lastIndexOf(".")+1);
		    		if(!"xls".equals(fileType))
		    		{
		    			resultState.setMessage("文件格式错误，请下载正确的模板导入试题！");
			    		return "";
		    		}
		    	}
		    	else
		    	{
		    		resultState.setMessage("文件名称为空！");
		    		return "";
		    	}
		    	String userId=((User)request.getSession().getAttribute(SessionConstant.USER_INFO)).getUserId();
				User user=new User();
				user.setUserId(userId);
		    	String resultMess=questionnaireService.importExcelQuestion(item,user);
		    	resultState.setMessage(resultMess);
		    }
		}
		
		return "";
	}

	/**
	 * 新增班级问卷关联关系
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-2-6
	 */
	@RequestMapping(value="/addClassQuestionnaire")
	public String addClassQuestionnaire(@ModelAttribute("result")QuestionnaireSurveyObjectValidCommond commond,
			HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("resultState")ResultState resultState)throws Exception{
		commond.setActiveState(1);
		commond.setCreateDate(new Date());
		Questionnaire questionnaire = new Questionnaire();
		questionnaire.setQuestionnaireId(request.getParameter("questionnaireId"));
		commond.setQuestionnaire(questionnaire);
		questionnaireService.save(commond);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("添加成功！");
		TrainingClassValidCommond classCommond = trainingClassService.find(commond.getSurveyObjectId());
		//生成二维码
		/*BuliderErWeiMa bewm= new BuliderErWeiMa();*/
		String url = request.getScheme() + "://"
		+ request.getServerName() + ":" + request.getServerPort()+""+request.getContextPath() +
				"/module/userquestionnaire/userLogin.do?method=scan&searchClassId="+commond.getSurveyObjectId()+"&classNum="+classCommond.getClassNum()+"&userId=";
		String encoderContent=url;
		TwoDimensionCode handler = new TwoDimensionCode();
		String imgPath = PathUtil.getWebRootPath()+"/erweima/classErweima/"+commond.getSurveyObjectId()+".png";
		File folder = new File(imgPath);       
		if(!(folder.exists()&&folder.isDirectory())){
			folder.mkdirs();
		}
		handler.encoderQRCode(encoderContent, "png", 10, imgPath); // 内容 图片路径
		// 图片格式 大小
		//String decoderContent = handler.decoderQRCode(imgPath); // 图片路径
		
		classCommond.setQuestionnaireUrl(url);
		trainingClassService.update(classCommond);
		return "";
	}
	
	/**
	 * 新增课程问卷关联关系
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author RongSL
	 * @createDate 2016-2-6
	 */
	@RequestMapping(value="/addCourseQuestionnaire")
	public String addCoursQuestionnaire(@ModelAttribute("result")QuestionnaireSurveyObjectValidCommond commond,
			HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("resultState")ResultState resultState)throws Exception{
		commond.setActiveState(1);
		commond.setCreateDate(new Date());
		Questionnaire questionnaire = new Questionnaire();
		questionnaire.setQuestionnaireId(request.getParameter("questionnaireId"));
		commond.setQuestionnaire(questionnaire);
		QuestionnaireSurveyObjectValidCommond c;
		
		//删除已有问卷及答案
		ClassCourseValidCommond validCommond=new ClassCourseValidCommond();
		validCommond.setCourseIds(commond.getSurveyObjectIds());
		classCourseService.deleteCourseQuestionnaire(validCommond);
		
		for(String id:commond.getSurveyObjectIds()){
			commond.setSurveyObjectId(id);
			c=new QuestionnaireSurveyObjectValidCommond();
			BeanUtils.copyProperties(commond, c);
			questionnaireService.save(c);
		}
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		resultState.setMessage("添加成功！");
		try{
			//生成二维码
			TrainingClassValidCommond classCommond = trainingClassService.find(questionnaireService.findClassId(commond));
			String url = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()+""+request.getContextPath() +
					"/module/userquestionnaire/userLogin.do?method=scan&searchClassId="+classCommond.getClassId()+"&classNum="+classCommond.getClassNum()+"&userId=";
			String encoderContent=url;
			TwoDimensionCode handler = new TwoDimensionCode();
			String imgPath = PathUtil.getWebRootPath()+"/erweima/classErweima/"+classCommond.getClassId()+".png";
			File folder = new File(imgPath);
			if(!(folder.exists()&&folder.isDirectory())){
				folder.mkdirs();
			}
			handler.encoderQRCode(encoderContent, "png", 10, imgPath); // 内容 图片路径
			classCommond.setQuestionnaireUrl(url);
			trainingClassService.update(classCommond);
		}catch(Exception e){
			System.out.println("生成二维码失败！");
		}		
		return "";
	}
}
